package com.comp490.shooter;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import android.content.Context;

public class ScoreConduit
{
	private static String FILENAME = "leadhammer_scores";
	private static String DEFAULT_CONTENT = "<?xml version=\"1.0\"?>"
			+ "<highscores>" + "<level1>" + "<name>PMP</name>"
			+ "<score>30000</score>" + "</level1><level1>" + "<name>GOR</name>"
			+ "<score>25000</score>" + "</level1><level1>" + "<name>LOS</name>"
			+ "<score>20000</score>" + "</level1><level1>" + "<name>JGZ</name>"
			+ "<score>18000</score>" + "</level1><level1>" + "<name>ARO</name>"
			+ "<score>17000</score>" + "</level1><level1>" + "<name>MNG</name>"
			+ "<score>16000</score>" + "</level1><level1>" + "<name>ABC</name>"
			+ "<score>15000</score>" + "</level1><level1>" + "<name>MNM</name>"
			+ "<score>14000</score>" + "</level1><level1>" + "<name>NOO</name>"
			+ "<score>13000</score>" + "</level1><level1>" + "<name>MON</name>"
			+ "<score>12000</score>" + "</level1>" + "</highscores>";

	public static List<ScoreStruct> readHighScores(Context context, int level)
	{
		try
		{
			ScoreStruct score;
			List<ScoreStruct> scoreSet = new ArrayList<ScoreStruct>();
			FileInputStream fis = context.openFileInput(FILENAME);
			DocumentBuilderFactory dbFactory = DocumentBuilderFactory
					.newInstance();
			DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
			Document doc = dBuilder.parse(fis);

			doc.getDocumentElement().normalize();

			NodeList nlist = doc.getElementsByTagName("level" + level);

			for (int i = 0; i < nlist.getLength(); i++)
			{
				Node node = nlist.item(i);
				if (Node.ELEMENT_NODE == node.getNodeType())
				{
					Element element = (Element) node;
					score = new ScoreStruct();
					score.setName(element.getElementsByTagName("name").item(0)
							.getChildNodes().item(0).getNodeValue());
					score.setScore(Long.parseLong(element
							.getElementsByTagName("score").item(0)
							.getChildNodes().item(0).getNodeValue()));
					scoreSet.add(score);
				}
			}
			fis.close();
			return scoreSet;
		}
		catch (SAXException e)
		{
			e.printStackTrace();
			return new ArrayList<ScoreStruct>();
		}
		catch (ParserConfigurationException e)
		{
			e.printStackTrace();
			return new ArrayList<ScoreStruct>();
		}
		catch (IOException e)
		{
			// write new XML if it doesnt exist
			try
			{
				FileOutputStream fos = context.openFileOutput(FILENAME,	Context.MODE_PRIVATE);
				fos.write(DEFAULT_CONTENT.getBytes());
				fos.close();
				return ScoreConduit.readHighScores(context, level);
			}
			catch (Exception g)
			{
				g.printStackTrace();
				return new ArrayList<ScoreStruct>();
			}
			
		}
	}

	public static void writeScores(Context context, String name, long score,
			int place, int level)
	{
		try
		{
			FileInputStream fis = context.openFileInput(FILENAME);
			DocumentBuilderFactory dbFactory = DocumentBuilderFactory
					.newInstance();
			DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
			Document doc = dBuilder.parse(fis);
			doc.getDocumentElement().normalize();

			NodeList nlist = doc.getElementsByTagName("level" + level);

			Node node = nlist.item(place); // Name
			NodeList playerNode = node.getChildNodes();
			playerNode.item(0).setTextContent(name);

			// Score
			playerNode.item(1).setTextContent(Long.toString(score));

			fis.close();

			// write the content into xml file
			FileOutputStream fos = context.openFileOutput(FILENAME,	Context.MODE_PRIVATE);
			TransformerFactory transformerFactory = TransformerFactory
					.newInstance();
			Transformer transformer = transformerFactory.newTransformer();
			DOMSource source = new DOMSource(doc);
			StreamResult result = new StreamResult(fos);
			transformer.transform(source, result);
			fos.close();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}

	}
}
